﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.ThuQuy;
using Entities;
using BLL.ThuQuy;

namespace WindowsForms.ThuQuy
{
    public partial class FormHoaDon : Form
    {
        static List<string> list = new List<string>();
        public static string mahoadon;
        public FormHoaDon()
        {
            InitializeComponent();
            //LoadListView(HoaDon.Objects.All().ToList());
        }
        private void LoadListView(List<HoaDon> list)
        {
            lvHoaDon.Items.Clear();
            foreach (var item in list)
            {
                ListViewItem lvitem = new ListViewItem(item.MaHD);
                lvitem.SubItems.Add(item.MaLoai.ToString() == "2" ? "Chi" : "Thu");
                lvitem.SubItems.Add(item.NgayLap.ToShortDateString());
                lvitem.SubItems.Add(item.TenNguoiLap);
                lvHoaDon.Items.Add(lvitem);
            }

        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            FormThemHoaDon frm = new FormThemHoaDon();

            frm.ShowDialog();
        }

        private void TimKiemtoolStripButton1_Click(object sender, EventArgs e)
        {
            FormTimKiemHoaDon frm = new FormTimKiemHoaDon();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }
        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (FormTimKiemHoaDon.state)
                {
                    string dataSource = FormTimKiemHoaDon.data;
                    List<HoaDon> dshd = HoaDon.Objects.Find(p => p.MaHD.Contains(dataSource)).ToList();
                    if (dshd.Count > 0)
                    {
                        LoadListView(dshd);
                    }
                    else
                        MessageBox.Show("Không tồn tại hóa đơn có mã: " + dataSource);
                }
            }
            catch (Exception)
            {


            }

        }

        private void ThemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormChiTietHoaDon frm = new FormChiTietHoaDon();
              
            if (!String.IsNullOrWhiteSpace(mahoadon))
            {
                FormChiTietHoaDon.mahd = mahoadon;
                frm.ShowDialog();
            }
        }

        private void lvHoaDon_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (lvHoaDon.SelectedItems.Count > 0)
            //{
            //    mahoadon = lvHoaDon.SelectedItems[0].SubItems[0].Text;
            //    //messagebox.show(mahoadon);

            //}
            for (int i = 0; i < lvHoaDon.CheckedItems.Count; i++)
            {
                //if(lvHoaDon.CheckedItems[i].Checked)
                mahoadon = lvHoaDon.CheckedItems[i].SubItems[0].Text;
                list.Add(mahoadon);
                //MessageBox.Show(mahoadon);
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (var item in list)
            {

                if (HoaDon.Objects.Delete(h => h.MaHD == item))
                {
                    MessageBox.Show("Xóa Thành Công");
                    LoadListView(HoaDon.Objects.All().ToList());
                }
                else MessageBox.Show("Xóa Thất Bại");
            }
            
        }

        private void LoadtoolStripButton1_Click(object sender, EventArgs e)
        {
            LoadListView(HoaDon.Objects.All().ToList());
        }

        private void CapNhatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThemHoaDon.maHD = mahoadon;
            FormThemHoaDon.state = true;
            FormThemHoaDon fr = new FormThemHoaDon();
            fr.FormClosed += new FormClosedEventHandler(fr_FormClosed);
            fr.ShowDialog();

        }

        void fr_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadListView(HoaDon.Objects.All().ToList());
        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void thêmChiTiếtHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThemChiTietHoaDon fr = new FormThemChiTietHoaDon();
             if (!String.IsNullOrWhiteSpace(mahoadon))
            {
                FormThemChiTietHoaDon.mahoadon = mahoadon;
                fr.ShowDialog();
            }
        }
    }
}
